/* 全局变量 */
var dev21sun_is_import_dialog = false;// 是否引入dialog
var service_domain = "http://service.21-sun.com";
/* 全局变量 */

jQuery.Dev21sun = {
    // flv组件
    flv: function(opts){
        var defualts = {
            flv: '',
            selecter: 'flv_id',
            width: 300,
            height: 170,
            img: '',
            allowfullscreen: true,
            auto: false
        };
        opts = jQuery.extend({}, defualts, opts);
        var swf = new SWFObject("/plugin/video/flv/flvplayer.swf", "single", opts.width, opts.height, "7");
        swf.addParam("allowfullscreen", opts.allowfullscreen);
        swf.addVariable("file", opts.flv);
        swf.addVariable("image", opts.img);
        swf.addVariable("width", opts.width);
        swf.addVariable("height", opts.height);
        swf.addVariable("autostart", opts.auto);
        swf.write(opts.id);
    },
    // 登录组件
    login: function(opts){
        var defualts = {
            url: 'http://member.21-sun.com/interface/login_and_regist/login.jsp',
            width: 450,
            title: '用户登录',
            height: 358,
            buttons: null
        };
        opts = jQuery.extend({}, defualts, opts);
        clickOpen(opts);
    },
    // 注册组件
    register: function(opts){
        var defualts = {
            url: 'http://member.21-sun.com/interface/login_and_regist/regist.jsp',
            width: 668,
            top: 40,
            title: '用户注册',
            height: 568,
            buttons: null
        };
        opts = jQuery.extend({}, defualts, opts);
        clickOpen(opts);
    },
    include: function(file){
        var files = typeof file == "string" ? [file] : file;
        for (var i = 0; i < files.length; i++) {
            var name = files[i].replace(/^\s|\s$/g, "");
            var att = name.split('.');
            var ext = att[att.length - 1].toLowerCase();
            var isCSS = ext == "css";
            var tag = isCSS ? "link" : "script";
            var attr = isCSS ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ";
            var link = (isCSS ? "href" : "src") + "='" + name + "'";
            if ($(tag + "[" + link + "]").length == 0) 
                document.write("<" + tag + attr + link + "></" + tag + ">");
        }
    }
};

/**
 * 注册单击弹出
 *
 * @param {}
 *            opts
 */
function clickOpen(opts){
    var defualts = {
        url: '',
        title: null,
        is_import: true,
        selecter: '',
        top: '20%',
        width: 800,
        height: 600,
        draggable: true,
        css: 'Default',
        buttons: {
            '关闭': true
        }
    };
    opts = jQuery.extend({}, defualts, opts);
    // 如果需要引入dialog
    if (opts.is_import) {
        // 如果没有引入，则进行引入
        if (!dev21sun_is_import_dialog) {
            // 引入默认jBox包
            var icArr = [];
            icArr.push(service_domain + "/plugin/dialog/jBox/Skins/" + opts.css + "/jbox.css");
            icArr.push(service_domain + "/plugin/dialog/jBox/jquery.jBox-2.3.min.js");
            jQuery.Dev21sun.include(icArr);
            // 导入设置为true
            dev21sun_is_import_dialog = true;
        }
        jQuery(function(){
            jQuery(opts.selecter).click(function(){
                jQuery.jBox.setDefaults({
                    defaults: {
                        border: 0
                    }
                });
                var url = opts.url;
                jQuery.jBox("iframe:" + url, {
                    title: opts.title,
                    width: opts.width,
                    height: opts.height,
                    top: opts.top,
                    draggable: opts.draggable,
                    iframeScrolling: false,
                    buttons: opts.buttons
                });
            });
        });
    }
}
